import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * 总行人力项目
 */
public class Rmyh {

    /**
     * 数据库地址
     */
    private static String jdbcUrl = "10.100.215.27:3030";

    /**
     * 数据库名称
     */
    private static String db_name = "PDB1";
    /**
     * 数据库账号
     */
    private static String db_username = "uprr";
    /**
     * 数据库密码
     */
    private static String db_password = "uprr";
    /**
     * 文件生成位置
     */
    private static  String path = "D:/gitWork/";
    /**
     * xml生成位置
     */
    private static  String  xmlpath =  "D:/gitWork/xml";

    /**
     * 模板位置
     */
    private static  String  temppath = "/templates/";
    /**
     * 涉及到的表
     */
    private static String[] tables = new String[]{
            "T4_FACT_TB_SUS_REPORT",
            "T4_FACT_TB_LAR_REPORT",
            "T4_FACT_TB_INS_UNIT",
            "T4_FACT_TB_INS_RTYPE",
            "T4_FACT_TB_INS_RSUR",
            "T4_FACT_TB_INS_RPOL",
            "T4_FACT_TB_INS_RPAY",
            "T4_FACT_TB_INS_RISK_NEW",
            "T4_FACT_TB_INS_RISK",
            "T4_FACT_TB_INS_RENEWAL",
            "T4_FACT_TB_INS_RCLA",
            "T4_FACT_TB_INS_RCHG",
            "T4_FACT_TB_INS_PERS",
            "T4_FACT_TB_INS_GPOL",
            "T4_FACT_TB_INS_FAV_CST",
            "T4_FACT_TB_INS_BO",
            "T4_FACT_TB_COMPANY",
            "T4_CFG_TABLENAME",
            "T4_CFG_NORMAL_LMRISKAPP",
            "T4_CFG_NORMAL_LDCOM",
            "T4_CFG_INPUT_LEVEL_ORG",
            "T4_CFG_INPUT_DATE_PARAMS",
            "T4_CFG_CREATED_FILE_STATE"
    };
    /**
     * 包名
     */
    private static String parentPackage="com.ccblife.report.module.rmyh";
    /**
     * api的包名
     */
    private static String apipackage="com.ccblife.report.uprr.api.module.rmyh";

    private static String author = "lixing";


    public static void main(String[] args) {
       createCommon();
        createDto();
        createParam();
    }


    public  static void  createCommon(){
        AutoGenerator autoGenerator=createGenerator();
        autoGenerator.template(createTemplateConfig());
        autoGenerator.packageInfo(createPackageConfig("common"));
        autoGenerator.strategy(createStrategyConfig());
        autoGenerator.strategy(createStrategyConfig());
        autoGenerator.injection(createInjectionConfig("common"));
        autoGenerator.global(createGlobalConfig());
        autoGenerator.execute();
    }
    public  static void  createDto(){
        AutoGenerator autoGenerator=createGenerator();
        autoGenerator.packageInfo(createPackageConfig("dto"));
        autoGenerator.strategy(createStrategyConfig());
        autoGenerator.injection(createInjectionConfig("dto"));
        autoGenerator.global(createGlobalConfig());
        autoGenerator.execute();
    }
    public  static void  createParam(){
        AutoGenerator autoGenerator=createGenerator();
        autoGenerator.packageInfo(createPackageConfig("param"));
        autoGenerator.strategy(createStrategyConfig());
        autoGenerator.injection(createInjectionConfig("param"));
        autoGenerator.global(createGlobalConfig());
        autoGenerator.execute();
    }


    private static AutoGenerator createGenerator(){
        return new AutoGenerator(createDataSource());
    }
    private static DataSourceConfig createDataSource(){
      return   new DataSourceConfig.Builder("jdbc:oracle:thin:@"+jdbcUrl+"/"+db_name,db_username,db_password).build();
    }

    private static TemplateConfig createTemplateConfig(){
       return new TemplateConfig.Builder()
                .entity(temppath+"entity.java")
                .service(temppath+"service.java")
                .serviceImpl(temppath+"serviceImpl.java")
                .mapper(temppath+"mapper.java")
                .xml(temppath+"mapper.xml")
                .controller(temppath+"controller.java")
                .build();
    }
    private static PackageConfig createPackageConfig(String type){
        Map output=new HashMap();
        if("common".equals(type)){
            output.put(OutputFile.xml,xmlpath);
        }else{
            output.put(OutputFile.entity,null);
            output.put(OutputFile.service,null);
            output.put(OutputFile.mapper,null);
            output.put(OutputFile.xml,null);
            output.put(OutputFile.serviceImpl,null);
            output.put(OutputFile.controller,null);
            if("param".equals(type)){
                output.put(OutputFile.other,path+"api/param");
            }else{
                output.put(OutputFile.other,path+"api/dto");
            }

        }
      return new PackageConfig.Builder().parent(parentPackage)
                .pathInfo(output)
                .build();
    }
    private static StrategyConfig createStrategyConfig(){
      return   new StrategyConfig.Builder()
                .addTablePrefix("R_")
                .addInclude(tables)
                .entityBuilder()
                .enableLombok()
                .fileOverride()
                .build();

    }

    private static InjectionConfig createInjectionConfig(String type) {
        Map map = new HashMap();
        map.put("apipackage", apipackage);
        InjectionConfig.Builder injectionConfig = new InjectionConfig.Builder();
        injectionConfig.customMap(map);

            injectionConfig.beforeOutputFile((tableInfo, objectMap) -> {
                ConfigBuilder configBuilder = (ConfigBuilder) objectMap.get("config");
                Map customFileMap = configBuilder.getInjectionConfig().getCustomFile();
                customFileMap.clear();
                String oldEntityName = tableInfo.getEntityName();
                if ("dto".equals(type)) {
                    customFileMap.put(oldEntityName + "Dto.java", temppath + "entityDto.java.vm");
                }
                if("param".equals(type)) {
                    customFileMap.put(oldEntityName + "AddParam.java", temppath + "entityAdd.java.vm");
                    customFileMap.put(oldEntityName + "UpdateParam.java", temppath + "entityUpdate.java.vm");
                    customFileMap.put(oldEntityName + "PageParam.java", temppath + "entityPageParam.java.vm");

                }

                char[] chars = oldEntityName.toCharArray();
                chars[0] += 32;
                String newEntityName = String.valueOf(chars);
                if(!"common".equals(type)){
                  tableInfo.setEntityName(newEntityName);
                }
                Map customMap = configBuilder.getInjectionConfig().getCustomMap();
                customMap.put("newEntityName", newEntityName);
            });

        return injectionConfig.build();
    }


    private static GlobalConfig createGlobalConfig(){
           return new GlobalConfig.Builder()
                   .outputDir(path)
                   .author(author)
                   .build();
       }
    }

